﻿using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Client.IService.LogManager;

namespace PLCS.App.ProcessSolution.ViewModels.Process;

public class ProcessDetailViewModel : HostDialogViewModel
{
    #region 编辑触发器实体

    private TriggerEditDto _triggerEditModel = new TriggerEditDto();

    public TriggerEditDto TriggerEditModel
    {
        get { return _triggerEditModel; }
        set { SetProperty(ref _triggerEditModel, value); }
    }

    #endregion 编辑触发器实体

    #region 触发条件集合

    private ObservableCollection<TriggerDto> _triggers;

    public ObservableCollection<TriggerDto> Triggers
    {
        get { return _triggers; }
        set { SetProperty(ref _triggers, value); }
    }

    #endregion 触发条件集合

    #region 添加一个触发事件

    public DelegateCommand AddItemCommand => new DelegateCommand(AddItem);

    private void AddItem()
    {
        if (Triggers == null)
            Triggers = new ObservableCollection<TriggerDto>();
        Triggers.Add(new TriggerDto()
        {
            CompareValue = TriggerEditModel.CompareValue,
            BackDataKey = TriggerEditModel.BackDataKey,
            BackDataPath = TriggerEditModel.BackDataPath,
            ComparisonType = TriggerEditModel.ComparisonType,
            Size = TriggerEditModel.Size,
            DataType = TriggerEditModel.DataType,
            DBNumber = TriggerEditModel.DBNumber,
            IpAddress = TriggerEditModel.IpAddress,
            Offset = TriggerEditModel.Offset,
            IsReadFromPLC = !string.IsNullOrEmpty(TriggerEditModel.IpAddress)
        });
        IsTriggerDialogOpen = false;
    }

    #endregion 添加一个触发事件

    #region 删除一个触发事件

    public DelegateCommand<TriggerDto> DeleteItemCommand => new DelegateCommand<TriggerDto>(DeleteItem);

    private void DeleteItem(TriggerDto model)
    {
        Triggers.Remove(model);
    }

    #endregion 删除一个触发事件

    #region 子弹窗标志位

    private bool _isTriggerDialogOpen;

    public bool IsTriggerDialogOpen
    {
        get { return _isTriggerDialogOpen; }
        set { SetProperty(ref _isTriggerDialogOpen, value); }
    }

    private bool _isWorkStepDialogOpen;

    public bool IsWorkStepDialogOpen
    {
        get { return _isWorkStepDialogOpen; ; }
        set { SetProperty(ref _isWorkStepDialogOpen, value); }
    }

    #endregion 子弹窗标志位

    private ProcessDto _model;
    private readonly IProcessService _processService;
    private readonly IObjectMapper<ProcessSolutionModule> _objectMapper;

    public ProcessDto Model
    {
        get { return _model; }
        set { SetProperty(ref _model, value); }
    }

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        if (parameters.ContainsKey("Value"))
        {
            Model = parameters.GetValue<ProcessDto>("Value");
            Title = "编辑";
            Triggers = Model.Triggers.ToObservable();
        }
        else
        {
            Title = "新增";
            Model = new ProcessDto();
        }
    }

    protected override async void Save()
    {
        try
        {
            SavebuttonIsEnabled = false;
            Model.Triggers = Triggers?.ToList();
            var process = _objectMapper.Map<ProcessDto, ProcessEditDto>(Model);
            if (process.Id != Guid.Empty)
            {
                await _processService.UpdateAsync(process);
                Log("修改", process);
            }
            else
            {
                await _processService.CreateAsync(process);
                Log("新增", process);
            }

            ShowMessage("操作成功");
            base.Save();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
        finally { SavebuttonIsEnabled = true; }
    }

    public ProcessDetailViewModel(
        IProcessService processService,
        IObjectMapper<ProcessSolutionModule> objectMapper)
    {
        _processService = processService;
        _objectMapper = objectMapper;
        View = "工序管理";
    }
}